﻿using EquationsBankSite.Models;
using EquationsBankSite.Models.Helpers;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DatabaseModule;

namespace EquationsBankSite.Controllers
{
    public class FieldsController : Controller
    {
        public static EquationsDBEntities entities = new EquationsDBEntities();

        #region Public Actions

        //
        // GET: /Fields/
        public ActionResult Index(int? pathId = null)
        {
            string name = null;
            string desc = null;
            string formula = null;
            string reference = null;
            string citation = null;
            string pathValue = null;

            if (pathId != null)
            {
                var path = entities.Equation_Path.FirstOrDefault(p => p.ID == pathId);
                if (path != null)
                {
                    var equation = path.Equation;
                    name = equation.Equation_Name
                            .Single(n => n.Value == path.Value)
                        .Equation_Name_Language
                            .Single(l => l.Name == "V_Equation_00101")
                        .Value;
                    desc = equation.Equation_Description
                            .Single(d => d.Value == path.Value)
                        .Equation_Description_Language
                            .Single(l => l.Name == "V_Equation_00501")
                        .Value;
                    formula = equation.Equation_Formula
                            .Single(f => f.Value == path.Value)
                        .Representation;
                    reference = equation.Equation_Reference
                            .Single(r => r.Value == path.Value)
                        .ReferenceValue;
                    citation = equation.Equation_Citation
                            .Single(c => c.Value == path.Value)
                        .CitationValue;
                    pathValue = path.Equation_Path_Language
                            .Single(v => v.Name == "V_Equation_00002")
                        .Value;
                }
            }

            var model = new FieldsModel
            {
                CategoryTree = HttpContext.Application["Fields"] as TreeNode<string>,
                EquationName = name,
                EquationDescription = desc,
                EquationFormula = formula,
                EquationCitation = citation,
                EquationReference = reference,
                EquationPath = pathValue,
            };

            return View(model);
        }

        #endregion Public Actions
    }
}
